package com.jwdf.uda.ui.fragment.stylist;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.StylistGuideEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GuideGetGuideListAction;
import com.jwdf.uda.ui.adapter.RecommendAdapter;
import com.jwdf.uda.ui.adapter.item.HomeRecommendItem;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * 类说明：
 */
public abstract class AbsStylistFragment extends SimpleListFragment {
    protected RecommendAdapter mAdapter;
    @Override
    protected void initViews() {
        mAdapter = new RecommendAdapter(getActivity(),false);
        mDataBinding.commonFragmentList.setAdapter(mAdapter);

        EventBusUtil.registerEvent(mAdapter);
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected void onListItemClick(int position) {

    }
    @Override
    protected void requestData(final boolean refresh) {
        if (refresh && mAdapter.getCount() == 0) {
            showProgressDialog();
        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(GuideGetGuideListAction.newInstance(getContext(), getGuideListFilterType(), offset).request(new AHttpService.IResCallback<PbBean.PbResGuideGetDesignerGuideList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideGetDesignerGuideList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_operation))) {
                        mDataBinding.commonFragmentList.stopRefresh();
                        mDataBinding.commonFragmentList.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }
    protected abstract PbBean.PbSignGuideListFilterType getGuideListFilterType();
    /**
     * @param refresh
     * @param body
     */
    protected void doSuccessResponse(boolean refresh, PbBean.PbResGuideGetDesignerGuideList body) {
        if (body != null) {
            if (refresh) {
                mAdapter.setData(HomeRecommendItem.convert(body.getGuideListList()));
            } else {
                mAdapter.append(HomeRecommendItem.convert(body.getGuideListList()));
            }
            if (body.getGuideListList().size() == 0) {
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
            }
            mDataBinding.commonFragmentList.setPullLoadEnable(body.getGuideListList().size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    protected boolean showHeadDivider() {
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregisterEvent(mAdapter);
        EventBusUtil.unregisterEvent(this);
    }
    public void onEventMainThread(StylistGuideEvent event) {
        requestData(true);
    }
}
